package com.applet.service.impl;

import com.applet.annotation.SystemServerLog;
import com.applet.entity.request.*;
import com.applet.entity.response.GetLockInfoResponse;
import com.applet.entity.response.GetLockInfosResponse;
import com.applet.enums.ResultEnums;
import com.applet.mapper.LockInfoMapper;
import com.applet.model.LockBindingInfo;
import com.applet.model.LockInfo;
import com.applet.model.UserInfo;
import com.applet.service.LockService;
import com.applet.utils.AppletResult;
import com.applet.utils.ResultUtil;
import com.applet.utils.common.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LockServiceImpl extends BaseServiceImpl implements LockService{

    public static final Integer pageSize = 5;

    private static final Logger LOGGER= LoggerFactory.getLogger(LockServiceImpl.class);

    @SystemServerLog(funcionExplain = "获取锁信息")
    @Override
    public AppletResult getLockInfo(GetLockInfoRequest getLockInfoRequest){

        LockInfo lockInfo = lockInfoMapper.getLockInfoByLockNo(getLockInfoRequest.getLockNo());
        if(lockInfo != null){
            GetLockInfoResponse request = new GetLockInfoResponse();
            request.setId(lockInfo.getId());
            request.setIccidNo(lockInfo.getIccidNo());
            request.setLockBluetoothMac(lockInfo.getLockBluetoothMac());
            request.setLockHardVersion(lockInfo.getLockHardVersion());
            request.setLockSoftVersion(lockInfo.getLockSoftVersion());
            request.setLockNo(lockInfo.getLockNo());
            request.setLockName(lockInfo.getLockName());
            request.setLockSeries(lockInfo.getLockSeries());
            request.setLockStatus(lockInfo.getLockStatus());
            request.setManufactureId(lockInfo.getManufactureId());
            request.setNewKey(lockInfo.getNewKey());
            request.setNewPassword(lockInfo.getNewPassword());
            request.setTerminalType(lockInfo.getTerminalType());
            request.setSimNo(lockInfo.getSimNo());
            return ResultUtil.success(ResultEnums.RETURN_SUCCESS,request);
        }else{
            return ResultUtil.error(ResultEnums.NULL_LOCK_INFO);
        }

    }

    @SystemServerLog(funcionExplain = "绑定锁信息")
    @Override
    public AppletResult bindingLock(BindingLockRequest bindingLockRequest){

        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(bindingLockRequest.getUserId());
        if(userInfo != null){
            LockInfo lockInfo = lockInfoMapper.getLockInfoByLockNo(bindingLockRequest.getLockNo());
            if(lockInfo != null){
                LockBindingInfo info = lockBindingInfoMapper.selectByLockId(lockInfo.getId());
                if(info == null){
                    LockBindingInfo lockBindingInfo = new LockBindingInfo();
                    lockBindingInfo.setId(UuidUtil.getUuid());
                    lockBindingInfo.setLockId(lockInfo.getId());
                    lockBindingInfo.setUserId(userInfo.getId());
                    lockBindingInfo.setCreateDate(new Date());
                    lockBindingInfo.setDelFlag(0);
                    lockBindingInfoMapper.insertSelective(lockBindingInfo);

                    if(!StringUtils.isEmpty(bindingLockRequest.getLockName())){
                        lockInfo.setLockName(bindingLockRequest.getLockName());
                        lockInfoMapper.updateByPrimaryKeySelective(lockInfo);
                    }
                    return ResultUtil.success(ResultEnums.RETURN_SUCCESS_BINDING_LOCK,null);
                }else {
                    return ResultUtil.error(ResultEnums.LOCK_REPEAT_BINDING);
                }

            }else{
                return ResultUtil.error(ResultEnums.NULL_LOCK_INFO);
            }
        }else{
            return ResultUtil.error(ResultEnums.NULL_USER_INFO);
        }
    }

    @SystemServerLog(funcionExplain = "获取锁列表信息")
    @Override
    public AppletResult getLockInfos(GetLockInfosRequest getLockInfosRequest){

        GetLockInfosResponse getLockInfosResponse = new GetLockInfosResponse();
        List<GetLockInfoResponse> res = new ArrayList<>();

        int count = lockBindingInfoMapper.getCountByUserId(getLockInfosRequest.getUserId());

        if(count == 0){
            return ResultUtil.error(ResultEnums.NULL_LOCK_INFOS);
        }

        List<LockBindingInfo> lockBindingInfoList = lockBindingInfoMapper.
                getLockBingdingInfos(getLockInfosRequest.getUserId(),(getLockInfosRequest.getCurrPage() - 1)*pageSize,pageSize);
        for (LockBindingInfo lockBindingInfo:lockBindingInfoList){
            GetLockInfoResponse request = new GetLockInfoResponse();
            request.setId(lockBindingInfo.getLockInfo().getId());
            request.setIccidNo(lockBindingInfo.getLockInfo().getIccidNo());
            request.setLockBluetoothMac(lockBindingInfo.getLockInfo().getLockBluetoothMac());
            request.setLockHardVersion(lockBindingInfo.getLockInfo().getLockHardVersion());
            request.setLockSoftVersion(lockBindingInfo.getLockInfo().getLockSoftVersion());
            request.setLockNo(lockBindingInfo.getLockInfo().getLockNo());
            request.setLockName(lockBindingInfo.getLockInfo().getLockName());
            request.setLockSeries(lockBindingInfo.getLockInfo().getLockSeries());
            request.setLockStatus(lockBindingInfo.getLockInfo().getLockStatus());
            request.setManufactureId(lockBindingInfo.getLockInfo().getManufactureId());
            request.setNewKey(lockBindingInfo.getLockInfo().getNewKey());
            request.setNewPassword(lockBindingInfo.getLockInfo().getNewPassword());
            request.setTerminalType(lockBindingInfo.getLockInfo().getTerminalType());
            request.setSimNo(lockBindingInfo.getLockInfo().getSimNo());
            res.add(request);
        }
        getLockInfosResponse.setTotalPage(count/pageSize + 1);
        getLockInfosResponse.setLockList(res);
        return ResultUtil.success(ResultEnums.RETURN_SUCCESS, getLockInfosResponse);
    }

    @SystemServerLog(funcionExplain = "锁重命名")
    @Override
    public AppletResult renameLock(RenameLockRequest renameLockRequest){
        LockInfo lockInfo = lockInfoMapper.selectByPrimaryKey(renameLockRequest.getLockId());
        if(lockInfo != null){
            lockInfo.setLockName(renameLockRequest.getLockName());
            lockInfoMapper.updateByPrimaryKeySelective(lockInfo);
            return ResultUtil.success(ResultEnums.RETURN_SUCCESS,null);
        }else {
            return ResultUtil.error(ResultEnums.NULL_LOCK_INFO);
        }
    }

    @SystemServerLog(funcionExplain = "解绑锁")
    @Override
    public AppletResult removeLock(RemoveLockRequest removeLockRequest){
        LockBindingInfo lockBindingInfo = lockBindingInfoMapper.
                selectByUserIdAndLockId(removeLockRequest.getUserId(),removeLockRequest.getLockId());
        if(lockBindingInfo != null){
            lockBindingInfo.setDelFlag(1);
            lockBindingInfoMapper.updateByPrimaryKeySelective(lockBindingInfo);
            return ResultUtil.success(ResultEnums.RETURN_SUCCESS,null);
        }else {
            return ResultUtil.error(ResultEnums.NULL_LOCK_INFO);
        }
    }
}
